//
//  NSString+SMCategory.m
//  DiDi
//
//  Created by huangtie on 2017/3/19.
//  Copyright © 2017年 Sinmore. All rights reserved.
//

#import "NSString+SMCategory.h"

@implementation NSString (SMCategory)

+ (NSString *)replaceUnicode:(NSString *)string {
    if (!string.length) {
        return @"";
    }
    NSString *tempStr1  = [string stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2  = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3  = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData   *tempData  = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
    NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData
                                                                    options:NSPropertyListImmutable
                                                                     format:NULL
                                                                      error:NULL];
    if (!returnStr) {
        return string;
    }
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}

+ (NSString *)currentTimestamp
{
    return [NSString stringWithFormat:@"%@",@((NSInteger)[[NSDate date] timeIntervalSince1970])];
}

+ (double)todayFirstSecondTimeStamp{
    double currentTime = [[NSDate date] timeIntervalSince1970];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"A"];//得到的是毫秒值
    //此刻是今天第几秒
    double todaySecond = [[formatter stringFromDate:[NSDate date]] doubleValue] / 1000;
    //今天第一秒
    double todayFirstSecond = currentTime - todaySecond;
    return todayFirstSecond;
}

+ (NSString *)yearMonthWeekFromTimestamp:(NSString *)timestamp
{
    NSDictionary *timeGather = [NSString timeGatherFromTimestamp:timestamp];
    
    return [NSString stringWithFormat:@"%@月%@日 %@", timeGather[@"month"],timeGather[@"day"],timeGather[@"week"]];
}

+ (NSString *)timeStringToTimeString:(NSString *)timeString fromFormat:(NSString *)fromFormat toFormat:(NSString *)toFormat{
    NSString *timeStamp = [NSString stringToTimeStamp:timeString dateFormat:fromFormat];
    return [NSString timeStampToString:timeStamp dateFormat:toFormat];
}

+ (NSString *)weekCharFromTimestamp:(NSString *)timestamp
{
    if (!timestamp.length) {
        return @"";
    }
    NSArray *weekChars = [NSArray arrayWithObjects:[NSNull null], @"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    [calendar setTimeZone:timeZone];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitWeekday |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger week = [comps weekday];
    NSString *weekChar = @"星期";
    if (week < weekChars.count) {
        weekChar = [weekChars objectAtIndex:week];
    }
    return weekChar;
}

+ (NSDictionary *)timeGatherFromTimestamp:(NSString *)timestamp
{
    if (!timestamp.length) {
        return nil;
    }
    NSArray *weekChars = [NSArray arrayWithObjects:[NSNull null], @"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    [calendar setTimeZone:timeZone];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitWeekday |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:date];
    
    NSInteger year=[comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSInteger hour = [comps hour];
    NSInteger minute = [comps minute];
    NSInteger second = [comps second];
    
    NSInteger week = [comps weekday];
    NSString *weekChar = @"星期";
    if (week < weekChars.count) {
        weekChar = [weekChars objectAtIndex:week];
    }
    
    NSMutableDictionary *timeGather = [NSMutableDictionary dictionary];
    [timeGather setObject:@(year) forKey:@"year"];
    [timeGather setObject:[NSString stringWithFormat:@"%02d",(int)month] forKey:@"month"];
    [timeGather setObject:[NSString stringWithFormat:@"%02d",(int)day] forKey:@"day"];
    [timeGather setObject:[NSString stringWithFormat:@"%02d",(int)hour] forKey:@"hour"];
    [timeGather setObject:[NSString stringWithFormat:@"%02d",(int)minute] forKey:@"minute"];
    [timeGather setObject:[NSString stringWithFormat:@"%02d",(int)second] forKey:@"second"];
    [timeGather setObject:weekChar forKey:@"week"];
    return timeGather;
}

+ (NSString *)hourMinuteFromTimestamp:(NSString *)timestamp
{
    NSDictionary *timeGather = [NSString timeGatherFromTimestamp:timestamp];
    return [NSString stringWithFormat:@"%@:%@", timeGather[@"hour"],timeGather[@"minute"]];
}

//时间戳转换为可读时间
+ (NSString *)timeStampToString:(NSString *)timestamp dateFormat:(NSString *)dateFormat
{
    if (!timestamp || !timestamp.length || timestamp.length < 10 || !dateFormat.length)
    {
        return @"";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:dateFormat];
    
    //截取前十位  （由于时间戳的时13位的  所以要截取 得到10位的）
    NSString *time = [timestamp substringToIndex:10];
    // 时间戳转时间的方法   将前面nsstring的time幻化成int型之后  赋值给  nsstring  为了判断之后截取需要的时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[time integerValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

+ (NSString *)stringToTimeStamp:(NSString *)string dateFormat:(NSString *)dateFormat
{
    if (!string.length || !dateFormat.length)
    {
        return nil;
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:dateFormat];
    NSDate *date = [formatter dateFromString:string];
    return [NSString stringWithFormat:@"%@",@([date timeIntervalSince1970])];
}

/* xxxx-xx-xx xx:xx:xx 转成 今天xx:xx / 昨天xx:xx */
+ (NSString *)messageTimeWithTimeString:(NSString *)timeString{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    double timeStamp = [[NSString stringToTimeStamp:timeString dateFormat:@"yyyy-MM-dd HH:mm:ss"] doubleValue];
    double todayFirstSecond = [NSString todayFirstSecondTimeStamp];
    if (timeStamp > todayFirstSecond) {
        [formatter setDateFormat:@"HH:mm"];
    }else if (timeStamp > todayFirstSecond - 3600 * 24){
        [formatter setDateFormat:@"昨天 HH:mm"];
    }else if (timeStamp > todayFirstSecond - 3600 * 48){
        [formatter setDateFormat:@"前天 HH:mm"];
    }else{
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeStamp]];
}

+ (NSString *)transformDistance:(NSInteger)distance
{
    if (distance < 1000) {
        return STR_TO_STR(INT_TO_STR(distance), @"米");
    }else{
        NSString *d = [NSString stringWithFormat:@"%.2f",(CGFloat)distance / 1000.0];
        return STR_TO_STR(d,@"千米");
    }
}

+ (BOOL)isWeekend:(NSString *)timestamp
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar isDateInWeekend:[NSDate dateWithTimeIntervalSince1970:timestamp.integerValue]];
}

@end
